package org.wwb.antbox.center.web.controller.sys.user;

import org.ant.commons.models.model.BaseIdModel;
import org.ant.commons.models.result.BaseEditResult;
import org.ant.commons.models.result.BasePageResult;
import org.ant.commons.models.result.BaseResult;
import org.ant.model.common.session.SessionUser;
import org.ant.shiro.jwt.TokenManagerShiro;
import org.ant.shiro.session.SessionManagerShiro;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wwb.antbox.center.web.Router;
import org.wwb.antbox.user.iservice.ServiceUserTool;
import org.wwb.antbox.user.model.passport.view.RoleNamesAndPermissionsView;
import org.wwb.antbox.user.model.user.entity.SysUser;
import org.wwb.antbox.user.model.user.model.SysUserAddModel;
import org.wwb.antbox.user.model.user.model.SysUserDelModel;
import org.wwb.antbox.user.model.user.model.SysUserEditModel;
import org.wwb.antbox.user.model.user.model.SysUserPageModel;
import org.wwb.antbox.user.model.user.view.SysUserPageView;
import org.wwb.antbox.user.model.user.view.UserInfoView;
import org.wwb.antbox.user.model.userrole.model.SysUserRoleAddModel;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2019/9/22 12:39
 * @Description: 系统用户控制器
 */
@RestController
public class SysUserController {

    @Autowired
    private TokenManagerShiro tokenManagerShiro;

    /**
     * 获取当前用户
     * @return BaseResult
     */
    @GetMapping(value = Router.SysUser.GET_SYS_USER)
    public BaseResult<SessionUser> getSysUser(){
        SessionUser sessionUser = SessionManagerShiro.getSessionUser();
        BaseResult<SessionUser> baseResult = new BaseResult();
        baseResult.setModel(sessionUser);
        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     * 获取系统用户分页列表
     * @param sysUserPageModel
     * @return BasePageResult<SysUserPageView>
     */
    @PostMapping(value = Router.SysUser.GET_SYS_USER_PAGE)
    public BasePageResult<SysUserPageView> getSysUserPage(@RequestBody @Valid SysUserPageModel sysUserPageModel){
        return ServiceUserTool.sysUserService().getSysUserPage(sysUserPageModel);
    }

    /**
     * 添加系统用户
     * @param sysUserAddModel
     * @return BasePageResult<Integer>
     */
    @PostMapping(value = Router.SysUser.SAVE_SYS_USER)
    public BaseResult<Integer> saveSysUser(@RequestBody @Valid SysUserAddModel sysUserAddModel){
        return ServiceUserTool.sysUserService().saveSysUser(sysUserAddModel);
    }

    /**
     * 删除系统用户
     * @param sysUserDelModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.SysUser.DEL_SYS_USER)
    public BaseResult<Integer> delSysUser(@RequestBody @Valid SysUserDelModel sysUserDelModel){
        return ServiceUserTool.sysUserService().delSysUser(sysUserDelModel);
    }

    /**
     * 跳转修改系统用户
     * @param id 用户id
     * @return BaseEditResult
     */
    @GetMapping(value = Router.SysUser.DO_EDIT_SYS_USER)
    public BaseEditResult doEditSysUser(@RequestParam("id") Integer id){
        return ServiceUserTool.sysUserService().doEditSysUser(new BaseIdModel(id));
    }

    /**
     * 修改系统用户
     * @param sysUserEditModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.SysUser.EDIT_SYS_USER)
    public BaseResult<Integer> editSysUser(@RequestBody @Valid SysUserEditModel sysUserEditModel){
        return ServiceUserTool.sysUserService().editSysUser(sysUserEditModel);
    }

    /**
     * 获取用户角色关系列表
     * @param userId 用户id
     * @return BaseResult<List<String>> 角色Id集合
     */
    @GetMapping(value = Router.SysUser.GET_SYS_USER_ROLE_LIST)
    public BaseResult<List<String>> getSysUserRoleList(@RequestParam("userId") String userId){
        return ServiceUserTool.sysUserRoleService().getSysUserRoleIdList(userId);
    }

    /**
     * 保存用户角色关系
     * @param sysUserRoleAddModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.SysUser.SAVE_SYS_USER_ROLE)
    public BaseResult<Integer> saveSysUserRole(@RequestBody @Valid SysUserRoleAddModel sysUserRoleAddModel){
        return ServiceUserTool.sysUserRoleService().saveSysUserRole(sysUserRoleAddModel);
    }

    /**
     * 重置用户密码
     * @param sysUserDelModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.SysUser.EDIT_SYS_USER_PWD)
    public BaseResult<Integer> editSysUserPwd(@RequestBody @Valid SysUserDelModel sysUserDelModel){
        return ServiceUserTool.sysUserService().editSysUserPwd(sysUserDelModel);
    }


    /**
     * 获取用户信息
     * @param userId 用户id
     * @param loginName 登录账号
     * @return BaseListResult<UserRoleListView>
     */
    @GetMapping(value = Router.SysUser.GET_SYS_USER_INFO)
    public BaseResult<UserInfoView> getSysUserInfo(@RequestParam("userId") String userId, @RequestParam("loginName") String loginName){

        UserInfoView userInfoView;

        /**
         * 先从Redis中获取
         */
        SessionUser redisSessionUser = tokenManagerShiro.getCurrentUser(loginName);
        if(!StringUtils.isEmpty(redisSessionUser.getId())){
            userInfoView = UserInfoView.getUserInfoView(redisSessionUser);
        }else {
            // Redis中没有 从数据库获取
            BaseResult<SysUser> sysUserBaseResult = ServiceUserTool.sysUserService().getUserById(new BaseIdModel<>(userId));
            userInfoView = UserInfoView.getUserInfoView(sysUserBaseResult.getModel());

            // 权限和角色信息
            BaseResult<RoleNamesAndPermissionsView> userRoleNamesAndPermissions = ServiceUserTool.passportService().getUserRoleNamesAndPermissions(new BaseIdModel<>(userId));
            userInfoView.setPermissions(userRoleNamesAndPermissions.getModel().getAllPermissions());
            userInfoView.setRoleNames(userRoleNamesAndPermissions.getModel().getUserRoleNames());
        }

        BaseResult<UserInfoView> baseResult = new BaseResult<>();
        baseResult.setModel(userInfoView);
        baseResult.setSuccess(true);
        return baseResult;
    }

}
